import Taro from '@tarojs/taro'
import app from '../../../../main.js'
let myJs = {
    getData: function() {
        this.state = {
            type: this.$router.params.type,
            relate_id: this.$router.params.relate_id,
            sender_type: '医生',
            receiver_type: '患者',
            data: '',
            value: '',
            sender_id: app.current.id,
            receiver_id: this.$router.params.receiver_id,
            mySelfId: '',
            otherId: '',
            url: app.apiUri + '/web/instant_chat_record/create',
            socketTask: null,
            messages: [],
            startTime: '',
            recordStatus: false,
            recorderManager: Taro.getRecorderManager()
        };
        return this.state;
    },
    // 从服务器获取聊天数据
    getMessages() {
        var me = this;
        var data = {
            offset: this.offset, 
            length: this.length,
            sender_type: this.state.sender_type,
            sender_id: this.state.sender_id,
            receiver_type: this.state.receiver_type,
            receiver_id: this.state.receiver_id
        }
        app.get('web/instant_chat_record/manyWithPerson', data, function(res) {
            Taro.stopPullDownRefresh(); // 停止下拉刷新

            var messages = [];
            var list = res.data.result.list;
            me.total = res.data.result.total;
            for (var i = 0; i < list.length; i++) {
                var item = list[i];
                try {
                    var content = JSON.parse(item.content);
                    var data = content.data;
                    if (typeof data == 'string' && app.inArray(['audio', 'article', 'flow', 'flag', 'wz_illness_form'], content.type)) {
                        data = JSON.parse(data);
                    }
                } catch (error) {
                    console.log('出错啦', item.content);
                    continue;
                }
                var message = {
                    type: content.type,
                    data: data,
                    is_play: false,
                    isMyself: item.sender_type == '医生' ? true : false,
                    sender_id: item.sender_id,
                    sender_head_image: item.sender_type == '医生' ? item.sender_head_image : item.receiver_head_image,
                    receiver_id: item.receiver_id,
                    receiver_head_image: item.receiver_type == '医生' ? item.sender_head_image : item.receiver_head_image 
                };
                messages.unshift(message);
            }

            messages = messages.concat(me.state.messages);

            me.setState({
                messages: messages
            });

            if (me.offset == 0) {
                me.toPageBottom();
            }
        })
    },
    // 回到页面底部
    toPageBottom() {
        console.log('pagebottom');
        var query = Taro.createSelectorQuery()
        query.select('#page_v').boundingClientRect()
        query.selectViewport().scrollOffset()
        query.exec(function(res){
            Taro.pageScrollTo({
                scrollTop: res[0].height,
                duration: 300
            })
        })
    },
    componentWillMount () {
        if(this.init) {
            this.init();
        }
    },
    navigateTo(url) {
        app.navigateTo(url);
    },
    handleChange(v) {
        this.setState({
            value: v,
            data: v
        })
    },
    handleTextareaChange(name, e) {
        var names = name.split('.');
        var n = names[0];
        var temp = this.state;
        for(var i = 0; i < names.length - 1; i++) {
            temp = temp[names[i]];
        }
        temp[names[i]] = e.target.value;
        this.setState({n: temp[names[i]]});
    },
    redirectTo(url) {
        app.redirectTo(url);
    },
    componentDidMount () {
        setTimeout(this.toPageBottom,500)
    },
    componentWillUnmount () {},
    componentDidHide () {}
}
export default myJs;
